import React, { useEffect, useState } from "react";
import { View, StatusBar, Text } from 'react-native';
import { useNavigation } from "@react-navigation/native";
import AsyncStorage from '@react-native-async-storage/async-storage';
import { useTranslation } from '@/app/utils/useTranslation';

const LoadingScreen = () => {
    const [isLogin, setIsLogin] = useState(false);
    const navigation = useNavigation<any>();
    const { t } = useTranslation();
    
    useEffect(() => {
        const checkLoginStatus = async () => {
            try {
                const value = await AsyncStorage.getItem('login');
                if (value) {
                    setIsLogin(value === 'true');
                    navigation.reset({
                        index: 0,
                        routes: [{ name: '(tabs)' }],
                    });
                }else{
                    navigation.reset({
                        index: 0,
                        routes: [{ name: 'pages/auth/LoginScreen' }],
                    });
                }
            } catch (e) {
                console.error('获取登录状态时出错', e);
                navigation.reset({
                    index: 0,
                    routes: [{ name: 'pages/auth/LoginScreen' }],
                });
            }
        };
        
        checkLoginStatus();
    }, [navigation]);
    
    return (
        <>
            <StatusBar
                animated={true} // 指定状态栏的变化是否应以动画形式呈现
                hidden={false} // 是否隐藏状态栏
                backgroundColor={'transparent'} // 状态栏的背景色
                translucent={true} // 指定状态栏是否透明
                barStyle={'dark-content'} // enum('default', 'light-content', 'dark-content')
            />
            <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                <Text>{t('loading.loading')}</Text>
            </View>
        </>
    );
};

export default LoadingScreen;
